﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.IO;
using System.Configuration;

namespace Axis.Web.Administrator.Global
{
    public class CEmail
    {
        protected string GetContentLotteryTemplate(string OrderFile)
        {
            try
            {
                string ret = string.Empty;
                StreamReader streamReader = File.OpenText(HttpContext.Current.Server.MapPath(string.Format("~/Views/template/{0}.txt", OrderFile)));
                ret = streamReader.ReadLine();
                while (streamReader.ReadLine() != null)
                {
                    ret += streamReader.ReadLine();
                }
                streamReader.Close();
                
              
                return ret;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

        public void SendMail(string mailTo, string mailSubject, string fileName, string fileAttach = null)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            string mailFrom = "admin@sotipha.com.vn";
            string mailPass = "0988334885";
            message.From = new MailAddress(mailFrom);
            message.To.Add(new MailAddress(mailTo));
            if (fileAttach != null)
                message.Attachments.Add(new Attachment(fileAttach));
            message.IsBodyHtml = true;
            message.Subject = mailSubject;
            message.Body = GetContentLotteryTemplate(fileName);           
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.sotipha.com.vn", 25);
            client.Credentials = new System.Net.NetworkCredential(mailFrom, mailPass);
            /* mail config */
            client.Port = 25;
            client.Host = "smtp.sotipha.com.vn";
            client.EnableSsl = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };
                client.Send(message);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
            }
            finally
            {
            }
        }

        public void SendMail2(string mailTo, string mailSubject, string Content, string fileAttach = null)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            string mailFrom = "admin@sotipha.com.vn";
            string mailPass = "0988334885";
            message.From = new MailAddress(mailFrom);
            message.To.Add(new MailAddress(mailTo));
            if (fileAttach != null)
                message.Attachments.Add(new Attachment(fileAttach));
            message.IsBodyHtml = true;
            message.Subject = mailSubject;
            message.Body = Content;
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.sotipha.com.vn", 25);
            client.Credentials = new System.Net.NetworkCredential(mailFrom, mailPass);
            /* mail config */
            client.Port = 25;
            client.Host = "smtp.sotipha.com.vn";
            client.EnableSsl = false;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };
                client.Send(message);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
            }
            finally
            {
            }
        }

    }
}
